package com.ziven.common.widget.alphabet;

import androidx.annotation.NonNull;

/**
 * @author Ziven
 */
abstract class AbstractToPinyin {

    @NonNull
    public String toIndexAlphabet(String string) {
        string = toFirstPinyin(string);
        return string.length() == 0
                ? ToPinyinUtil.EMPTY
                : string.substring(0, 1);
    }

    @NonNull
    public String toFirstPinyin(String string) {
        string = ToPinyinUtil.trim(string);
        return string.length() == 0
                ? ToPinyinUtil.EMPTY
                : ToPinyinUtil.empty(toPinyin(string.charAt(0)));
    }

    @NonNull
    public String toPinyin(String string, @NonNull String separator) {
        string = ToPinyinUtil.trim(string);
        int length = string.length();
        if(length == 0) {
            return ToPinyinUtil.EMPTY;
        }
        StringBuilder sb = new StringBuilder(length * 2);
        for (int i=0; i<length; i++) {
            sb.append(ToPinyinUtil.empty(toPinyin(string.charAt(i))));
            sb.append(separator);
        }
        return sb.toString();
    }


    /**
     * To pinyin
     * @param indexAlphabet index alphabet
     * @return Pin yin
     */
    @NonNull
    public abstract String toPinyin(char indexAlphabet);
}
